using System;
using System.IO;
using BizTalk.BuildGenerator.FileBuilders;

namespace BizTalk.BuildGenerator
{
    public sealed class BuildProcessController
    {
        public delegate void BuildProcessUpdate(int percentageComplete);

        public static event BuildProcessUpdate ProcessUpdate;

        /// <summary>
        /// The main method which causes all of the files to be build
        /// </summary>
        /// <param name="args"></param>
        public static void FullBuild(GenerationArgs args)
        {
            IFileBuilder fileBuilder;

            //This is no longer required as a path to them will be provided
            //Copy all of the files which dont need to be built
            //CopyResourceFiles(args.OutputFolder); 

            fileBuilder = new ApplicationPropertiesFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(10);

            fileBuilder = new CustomTargetsFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(20);

            fileBuilder = new CustomPropertiesFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(20);

            fileBuilder = new RemoveFromGacFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(30);

            fileBuilder = new BuildProjectFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(50);

            fileBuilder = new CommonApplicationTargetsFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(70);

            fileBuilder = new DebugCmdFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(80);

            fileBuilder = new QuickBuildDebugCmdFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(90);

            fileBuilder = new ReleaseCmdFileBuilder();
            fileBuilder.Build(args);
            ProcessUpdate(100);
        }


        public static void CopyResourceFiles(string destinationPath)
        {
            string SourcePath = AppDomain.CurrentDomain.BaseDirectory + @"\Resources";

            CopyFolder(SourcePath, destinationPath);
        }

        private static void CopyFolder(string sourcePath, string destinationPath)
        {
            DirectoryInfo destinationDirectory = new DirectoryInfo(destinationPath);
            CopyFiles(sourcePath, destinationPath);
            foreach (string dir in Directory.GetDirectories(sourcePath))
            {
                string[] folders = Path.GetFullPath(dir).Split(Path.DirectorySeparatorChar);
                string folderName = folders[folders.GetUpperBound(0)];
                DirectoryInfo subDirectory = destinationDirectory.CreateSubdirectory(folderName);
                CopyFolder(dir, subDirectory.FullName);
            }
        }

        private static void CopyFiles(string sourceDirectory, string destinationDirectory)
        {
            foreach (string file in Directory.GetFiles(sourceDirectory))
            {
                string destinationFilePath = Path.Combine(destinationDirectory, Path.GetFileName(file));
                if (!File.Exists(destinationFilePath))
                    File.Copy(file, destinationFilePath);
            }
        }
    }
}